using Furion.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Furion.Localization;
using Furion.SpecificationDocument;
using Furion.Web.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Furion.Web.Entry.Controllers;

[AllowAnonymous]
public class FileController : Controller
{
    private readonly IRepository<SysFile> _sysFileRepository;
    public FileController(IRepository<SysFile> sysFileRepository)
    {
        _sysFileRepository = sysFileRepository;
    }



    /// <summary>
    /// 显示图片
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [AllowAnonymous]
    public IActionResult ShowImage([Required] long Id)
    {
        var val = _sysFileRepository.DetachedEntities.Any(a => a.Id == Id);
        if (val)
        {
            var file = _sysFileRepository.DetachedEntities.Where(a => a.Id == Id).FirstOrDefault();
            string absolutely_path = App.Configuration["FileUpload:AbsolutelySecurityPath"];
            var savePath = absolutely_path + file.FilePath;

            //FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
            //byte[] data = new byte[(int)fs.Length];
            //fs.Read(data, 0, data.Length);                
            //string Base64Image= "data:image/png;base64," + Convert.ToBase64String(data);
            //return Base64Image;

            //创建一个文件流
            FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
            //定义一个buffer数组
            byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
            fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
            return File(buffer, @"image/gif");
        }
        else
        {
            return new EmptyResult();
        }
    }


}
